/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦，本文采用木兰宽松许可证第2版]
 * 
 * https://zhiqim.org/project/zhiqim_kit/zhiqim_git.htm
 *
 * ZhiqimGit is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.git.command;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.zhiqim.git.Git;
import org.zhiqim.git.GitCommand;
import org.zhiqim.git.refer.Ref;
import org.zhiqim.git.refer.RefUpdateResult;
import org.zhiqim.git.refer.RefUpdater;


/**
 * 标签删除命令
 * http://www.kernel.org/pub/software/scm/git/docs/git-tag.html
 * 
 * @version v1.0.0 @author zhichenggang 2017-3-12 新建与整理
 */
public class TagDeleteCommand extends GitCommand<List<String>>
{
    private final Set<String> tags = new HashSet<String>();

    public TagDeleteCommand(Git git)
    {
        super(git);
    }
    
    /******************************************************************************/
    //设置参数
    /******************************************************************************/
    
    public TagDeleteCommand setTags(String... tags)
    {
        this.tags.clear();
        for (String tagName : tags)
            this.tags.add(tagName);
        return this;
    }

    /******************************************************************************/
    //预处理&执行
    /******************************************************************************/
    
    protected List<String> execute() throws Exception
    {
        if (tags.isEmpty())
            return new ArrayList<String>(0);

        List<String> result = new ArrayList<String>();
        for (String tagName : tags)
        {
            if (tagName == null)
                continue;
            
            Ref ref = git.findRef(tagName);
            if (ref == null)
                continue;
            
            String refName = ref.getName();
            RefUpdater update = git.newUpdater(refName);
            update.setForceUpdate(true);
            
            RefUpdateResult deleteResult = update.delete();
            if (!isSuccess(deleteResult))
                throw new Exception("删除标签时异常结果：" + deleteResult.name());
            
            result.add(refName);
        }
        
        return result;
    }
    
    /******************************************************************************/
    // 私有方法
    /******************************************************************************/
    
    /** 判断结果是否成功 */
    private boolean isSuccess(RefUpdateResult deleteResult)
    {
        switch (deleteResult)
        {
        case IO_FAILURE:
        case LOCK_FAILURE:
        case REJECTED:
            return false;
        default:
            return true;
        }
    }
}
